import copy
import json
import time
from random import choice


class AnalyzeDataUtil:
    def __init__(self, datas):
        self.datas = datas

    def get_json_data(self, api_path):
        # 获取demo 数据
        with open('./response_data_demo.json', 'r', encoding='utf-8') as fp:
            data_demo = json.load(fp)
            response_data_demo = data_demo[api_path]
            fp.close()
            return response_data_demo

    def analyze_data(self, data, result=""):
        data_a = data
        # 字典类型
        if isinstance(data_a, dict):
            for k, v in data_a.items():
                self.analyze_data(v, result+".%s" %str(k))
        # 列表或元组类型
        elif isinstance(data_a, (list, tuple)):
            for i in range(len(data_a)):
                self.analyze_data(data_a[i], result+".%s" %i)
        else:
            self.datas.append(result)
        return self.datas

    def update_type_string(self, dict_a, key):
        # 把相关字段修改为字符串类型
        data = copy.deepcopy(dict_a)
        new_key = key.lstrip(".")
        test = new_key.split('.')
        v = data
        for i in test[:-1]:
            if i.isdigit():
                v = v.__getitem__(int(i))
            else:
                v = v.__getitem__(i)
        if isinstance(v, list):
            v[int(test[-1])] = 'DSerstest'
        else:
            v[test[-1]] = 'DSerstest'
        return data

    def update_type_long_string(self, dict_a, key):
        # 把相关字段修改为长字符串类型
        data = copy.deepcopy(dict_a)
        new_key = key.lstrip(".")
        test = new_key.split('.')
        v = data
        for i in test[:-1]:
            if i.isdigit():
                v = v.__getitem__(int(i))
            else:
                v = v.__getitem__(i)
        if isinstance(v, list):
            v[int(test[-1])] = 'Hantek 4 Channel programmable linear DC regulated power supply with list+data recording+4 modes HDP4000'
        else:
            v[test[-1]] = 'Hantek 4 Channel programmable linear DC regulated power supply with list+data recording+4 modes HDP4000'
        return data

    def update_type_short_string(self, dict_a, key):
        # 把相关字段修改为短字符串类型
        data = copy.deepcopy(dict_a)
        new_key = key.lstrip(".")
        test = new_key.split('.')
        v = data
        for i in test[:-1]:
            if i.isdigit():
                v = v.__getitem__(int(i))
            else:
                v = v.__getitem__(i)
        if isinstance(v, list):
            v[int(test[-1])] = 'D'
        else:
            v[test[-1]] = 'D'
        return data

    def update_type_number(self, dict_a, key):
        # 把相关字段修改为数字类型
        data = copy.deepcopy(dict_a)
        new_key = key.lstrip(".")
        test = new_key.split('.')
        v = data
        for i in test[:-1]:
            if i.isdigit():
                v = v.__getitem__(int(i))
            else:
                v = v.__getitem__(i)
        if isinstance(v, list):
            v[int(test[-1])] = 20230101
        else:
            v[test[-1]] = 20230101
        return data

    def update_type_null(self, dict_a, key):
        # 把相关字段修改为空数据 
        data = copy.deepcopy(dict_a)
        new_key = key.lstrip(".")
        test = new_key.split('.')
        v = data
        for i in test[:-1]:
            if i.isdigit():
                v = v.__getitem__(int(i))
            else:
                v = v.__getitem__(i)
        if isinstance(v, list):
            v[int(test[-1])] = ""
        else:
            v[test[-1]] = ""
        return data

    def update_type_float(self, dict_a, key):
        # 把相关字段修改为浮点数
        data = copy.deepcopy(dict_a)
        new_key = key.lstrip(".")
        test = new_key.split('.')
        v = data
        for i in test[:-1]:
            if i.isdigit():
                v = v.__getitem__(int(i))
            else:
                v = v.__getitem__(i)
        if isinstance(v, list):
            v[int(test[-1])] = time.time()
        else:
            v[test[-1]] = time.time()
        return data

    def update_type_symbol(self, dict_a, key):
        # 把相关字段修改为特殊符号
        data = copy.deepcopy(dict_a)
        new_key = key.lstrip(".")
        test = new_key.split('.')
        v = data
        for i in test[:-1]:
            if i.isdigit():
                v = v.__getitem__(int(i))
            else:
                v = v.__getitem__(i)
        if isinstance(v, list):
            v[int(test[-1])] = """'!@#$%^&*()_\+{}|:"<>?+/*-"""
        else:
            v[test[-1]] = """'!@#$%^&*()_\+{}|:"<>?+/*-"""
        return data

    def update_type_WideChar(self, dict_a, key):
        # 把相关字段修改为全角符号
        data = copy.deepcopy(dict_a)
        new_key = key.lstrip(".")
        test = new_key.split('.')
        v = data
        for i in test[:-1]:
            if i.isdigit():
                v = v.__getitem__(int(i))
            else:
                v = v.__getitem__(i)
        if isinstance(v, list):
            v[int(test[-1])] = 'ＤＳｅｒｓ２０１８'
        else:
            v[test[-1]] = 'ＤＳｅｒｓ２０１８'
        return data

    def update_type_special_price(self, dict_a, key):
        # 把相关字段修改为特殊价格
        data = copy.deepcopy(dict_a)
        new_key = key.lstrip(".")
        test = new_key.split('.')
        v = data
        for i in test[:-1]:
            if i.isdigit():
                v = v.__getitem__(int(i))
            else:
                v = v.__getitem__(i)
        if isinstance(v, list):
            v[int(test[-1])] = '23,753.24'
        else:
            v[test[-1]] = '23,753.24'
        return data

    def update_type_currency_price(self, dict_a, key):
        # 把相关字段修改为带有货币方式的价格
        data = copy.deepcopy(dict_a)
        new_key = key.lstrip(".")
        test = new_key.split('.')
        v = data
        for i in test[:-1]:
            if i.isdigit():
                v = v.__getitem__(int(i))
            else:
                v = v.__getitem__(i)
        if isinstance(v, list):
            v[int(test[-1])] = 'US $264.01'
        else:
            v[test[-1]] = 'US $264.01'
        return data

    def update_unexpected_return(self, dict_a, *key):
        # 添加一个意外返回的字段
        data = copy.deepcopy(dict_a)
        add_dict = {"testliuguofeng": 'DSers2023'}
        data.update(add_dict)
        return data

    def delete_field(self, dict_a, key):
        # 删除相关字段
        data = copy.deepcopy(dict_a)
        new_key = key.lstrip(".")
        test = new_key.split('.')
        v = data
        for i in test[:-1]:
            if i.isdigit():
                v = v.__getitem__(int(i))
            else:
                v = v.__getitem__(i)
        if isinstance(v, list):
            del v[int(test[-1])]
        else:
            del v[test[-1]]
        return data

    def return_null(self, dict_a, *key):
        # 返回空数据
        return {}

    # def big_data(self, dict_a, key):
    #     # 返回超大数据
    #     return

    def update_true(self, dict_a, key):
        # 把相关字段修改为True
        data = copy.deepcopy(dict_a)
        new_key = key.lstrip(".")
        test = new_key.split('.')
        v = data
        for i in test[:-1]:
            if i.isdigit():
                v = v.__getitem__(int(i))
            else:
                v = v.__getitem__(i)
        if isinstance(v, list):
            v[int(test[-1])] = True
        else:
            v[test[-1]] = True
        return data

    def update_false(self, dict_a, key):
        # 把相关字段修改为False
        data = copy.deepcopy(dict_a)
        new_key = key.lstrip(".")
        test = new_key.split('.')
        v = data
        for i in test[:-1]:
            if i.isdigit():
                v = v.__getitem__(int(i))
            else:
                v = v.__getitem__(i)
        if isinstance(v, list):
            v[int(test[-1])] = False
        else:
            v[test[-1]] = False
        return data

    def update_none(self, dict_a, key):
        # 把相关字段修改为None
        data = copy.deepcopy(dict_a)
        new_key = key.lstrip(".")
        test = new_key.split('.')
        v = data
        for i in test[:-1]:
            if i.isdigit():
                v = v.__getitem__(int(i))
            else:
                v = v.__getitem__(i)
        if isinstance(v, list):
            v[int(test[-1])] = None
        else:
            v[test[-1]] = None
        return data

    def return_error(self, dict_a, *key):
        # 返回error 报错信息
        res = {
            "error_message": "error"}
        return res

    def return_original(self, dict_a, *key):
        # 不做任何修改
        res = dict_a
        return res

    def update_minus(self, dict_a, key):
        # 把相关字段修改为None
        data = copy.deepcopy(dict_a)
        new_key = key.lstrip(".")
        test = new_key.split('.')
        v = data
        for i in test[:-1]:
            if i.isdigit():
                v = v.__getitem__(int(i))
            else:
                v = v.__getitem__(i)
        if isinstance(v, list):
            v[int(test[-1])] = "-100"
        else:
            v[test[-1]] = "-100"
        return data

    def update_long_number(self, dict_a, key):
        # 把相关字段修改为None
        data = copy.deepcopy(dict_a)
        new_key = key.lstrip(".")
        test = new_key.split('.')
        v = data
        for i in test[:-1]:
            if i.isdigit():
                v = v.__getitem__(int(i))
            else:
                v = v.__getitem__(i)
        if isinstance(v, list):
            v[int(test[-1])] = 12345678901234567890
        else:
            v[test[-1]] = 12345678901234567890
        return data

    def update_alphabet_number(self, dict_a, key):
        # 把相关字段修改为None
        data = copy.deepcopy(dict_a)
        new_key = key.lstrip(".")
        test = new_key.split('.')
        v = data
        for i in test[:-1]:
            if i.isdigit():
                v = v.__getitem__(int(i))
            else:
                v = v.__getitem__(i)
        if isinstance(v, list):
            v[int(test[-1])] = "bqa49860e4rllmptor3g"
        else:
            v[test[-1]] = "bqa49860e4rllmptor3g"
        return data

    def random_function(self):
        # 随机选择执行一条用例
        functions_list = [generated_data.update_type_string,
                          generated_data.update_type_long_string,
                          generated_data.update_type_short_string,
                          generated_data.update_type_number,
                          generated_data.update_type_null,
                          generated_data.update_type_float,
                          generated_data.update_type_symbol,
                          generated_data.update_type_WideChar,
                          generated_data.update_type_special_price,
                          generated_data.update_type_currency_price,
                          generated_data.update_unexpected_return,
                          generated_data.delete_field,
                          generated_data.return_null,
                          generated_data.update_true,
                          generated_data.update_false,
                          generated_data.update_none,
                          generated_data.return_error,
                          generated_data.return_original,
                          generated_data.update_minus,
                          generated_data.update_long_number,
                          generated_data.update_alphabet_number]
        random_function = choice(functions_list)
        return random_function


generated_data = AnalyzeDataUtil([])


if __name__ == '__main__':
    generated_data = AnalyzeDataUtil([])
    # print(generated_data.analyze_data(generated_data.get_json_data('Supply_Product_detail')))
    test = generated_data.update_type_symbol(generated_data.get_json_data('Supply_Product_detail'),'productId')
    print(test)

